package com.gaattc.service;

import com.gaattc.dao.OrderDao;
import com.gaattc.dao.OrderDaoImpl;
import com.gaattc.model.Goods;
import com.gaattc.model.GoodsSpecs;
import com.gaattc.model.User;
import com.gaattc.model.bo.*;
import com.gaattc.model.vo.*;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class OrderServiceImpl implements OrderService {

    private OrderDao orderDao = new OrderDaoImpl();

    private Gson gson = new Gson();

    @Override
    public Map<String, Object> ordersByPage(OrdersByPageBO ordersByPageBO) {
        //分页查询
        Integer count = orderDao.getTotalCount(ordersByPageBO);
        List<OrdersByPageVO> ordersByPageList =  orderDao.getOrdersByPage(ordersByPageBO);
        Map<String, Object> map = new HashMap<>();
        map.put("total", count);
        map.put("orders", ordersByPageList);
        return map;
    }

    @Override
    public int deleteOrder(int id) {
        return orderDao.deleteOrder(id);
    }

    @Override
    public OrderVO order(int id) {
        OrderVO orderVO = orderDao.getOrder(id);
        int goodsId = orderVO.getGoodsId();

        List<SpecVO> goodsSpecs = orderDao.getGoodsSpecs(goodsId);
        orderVO.setSpec(goodsSpecs);

        String specName = orderVO.getSpecName();
        orderVO.setCurSpec(orderDao.getOrderSpecId(specName));

        return orderVO;
    }

    @Override
    public void changeOrder(ChangeOrderBO changeOrderBO) {
        String specName = orderDao.getSpecName(changeOrderBO.getSpec());
        changeOrderBO.setSpecName(specName);
        orderDao.changeOrder(changeOrderBO);
    }

    @Override
    public void addOrder(AddOrderBO addOrderBO) {
        User user = orderDao.getUser(addOrderBO.getToken());
        GoodsSpecs goodsSpecs = orderDao.getGoodsSpecsByDetailId(addOrderBO.getGoodsDetailId());
        Goods goods = orderDao.getGoodsById(goodsSpecs.getGoodsId());
        orderDao.addOrder(addOrderBO, user, goodsSpecs, goods);
    }

    @Override
    public List<OrdersByStateVO> getOrderByState(int state, String token) {
        User user = orderDao.getUser(token);
        return orderDao.getOrderByState(state, user.getId());
    }

    @Override
    public int settleAccounts(List<CartAccountsBO> cartList) {
        return orderDao.settleAccounts(cartList);
    }

    @Override
    public void confirmReceive(int id) {
        orderDao.confirmReceive(id);
    }

    @Override
    public void sendComment(SendCommentBO sendCommentBO) {
        User user = orderDao.getUser(sendCommentBO.getToken());
        String specName = orderDao.getSpecName(sendCommentBO.getGoodsDetailId());
        orderDao.sendComment(sendCommentBO, user, specName);
    }
}
